package com.itdaan.portal.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @author www.itdaan.com
 */
public class PseudoOrginalUtil {

	private static List<String> words = null;

	public static String getPseudoOrginal(String text){
		text = HtmlUtil.deleteHtmlTag(text);
		text = replaceWord(text);
		return text;
	}
	
	//将文章重新排版
	public static String changeText(String text){
		String [] textes = text.split("。");
		Arrays.sort(textes);
		return Arrays.toString(textes);
	}
	
	//替换伪原创词汇
	public static String replaceWord(String text) {
		if (words == null || words.size() == 0) {
			words = new ArrayList<String>();
			InputStream is = PseudoOrginalUtil.class.getResourceAsStream("/words.txt");
			InputStreamReader isr = null;
			BufferedReader br = null;
			try {
				isr = new InputStreamReader(is,"utf8");                     
                br=new BufferedReader(isr);   
				String tempString = null;
				while ((tempString = br.readLine()) != null) {
					words.add(tempString);
				}
				is.close();
				isr.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(isr != null){
					try {
						isr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(br != null){
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		String temp = "";
		String temp1 = "";
		String temp2 = "";
		int size = words.size();
		for(int i = 0;i < size;i++){
			temp = words.get(i);
			temp1= temp.split(",")[0];
			temp2 = temp.split(",")[1];
			if(text.indexOf(temp1) != -1){
				text.replaceAll(temp1, temp2);
				continue;
			}else if(text.indexOf(temp2) != -1){
				text.replaceAll(temp2, temp1);
				continue;
			}
		}
		return text;
	}
	
	public static void main(String[] args) {
		String text = replaceWord("BufferedReaderBufferedReader则是比InputStreamReader更高级,它封裝了StreamReader类,一次读取取一行的字符");
		System.out.println(text);
	}

}
